<template>
	<view class="WebViewAuthorize">
		<web-view v-if="webview" @message="message"
			:src="'https://api.zhetaoke.com:10001/api/open_sc_publisher_save.ashx?inviter_code=E7QXVW&backurl='+encodeURIComponent('http://118.190.159.217:8089')+'&type=0'">
		</web-view>
		<u-loading-page loading-text="授权中" :loading="loading"></u-loading-page>
	</view>
</template>

<script>
	import {
		taobaoauthorization
	} from '@/api/home'
	export default {
		data() {
			return {
				loading: false,
				Url: '',
				webview: true
			}
		},
		onShow() {},
		onLoad(data) {
			this.Url = data.url
			//#ifdef H5
			let this_ = this
			window.addEventListener('message', function(event) {
				let strtype = Object.prototype.toString.call(event.data.data)
				if (strtype == '[object Object]') {
					// console.log('接收到消息', event.data.data.arg).
					this_.queryAjax(event.data.data.arg)
				}
			}, false);
			// #endif
		},
		methods: {
			queryAjax(obj) {
				this.loading = true;
				let objj = Object.assign({}, obj)
				objj.sid = objj.relation_id
				console.log(objj.relation_id)
				
				if (!objj.relation_id) {
					uni.showToast({
						title: decodeURI(objj.desc),
						duration: 2000,
						icon: 'none'
					});
					return
				}
				taobaoauthorization(objj).then(res => {
					uni.showToast({
						title: res.message,
						duration: 2000,
						icon: 'none'
					});
					if (res.success == 1) {
						this.$scope.$getAppWebview().children()[0].close()
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					}
				}).finally(err => {
					this.loading = false
				})
			},
			message(event) {
				if (event.detail.data[0]) {
					this.queryAjax(event.detail.data[0])
				}
			}
		}
	}
</script>

<style>
	.WebViewAuthorize {
		height: 100%;
	}

	page {
		height: 100%;
	}
</style>
